from typing import Optional, List

class MockSPI:
    """
    Mock SPI bus class
    Used for testing SPI communication without hardware
    """
    def __init__(self, baudrate: int = 1000000, polarity: int = 0, phase: int = 0) -> None:
        self.baudrate = baudrate
        self.polarity = polarity
        self.phase = phase
        self.transfer_history: List[bytes] = []
    def write_readinto(self, send: bytes, recv: bytearray) -> None:
        for i in range(len(send)):
            recv[i] = send[i] ^ 0xFF
        self.transfer_history.append(send)
        print(f"SPI transfer: sent {send.hex()}, received {recv.hex()}")

def spi_transfer_mock(spi: MockSPI, data: bytes) -> Optional[bytes]:
    try:
        recv = bytearray(len(data))
        spi.write_readinto(data, recv)
        return bytes(recv)
    except Exception as e:
        print(f"SPI transfer error: {e}")
        return None
